package com.hindua.tab.scr;

import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.hindua.adapter.ItemAdapter;
import com.hindua.ailaaicuaai.ChapterActivity;
import com.hindua.beans.Item;
import com.rtst.widget.actionbar.ISearchInterface;
import com.rtst.widget.tab.TabFragment;
import com.hindua.ailaaicuaai.R;

public class CategoryScr extends TabFragment implements ISearchInterface {
	private static final String TAG = "CategoryScr";
	ViewGroup mLayout;
	private AdView adView;
	private ArrayList<Item> items = new ArrayList<Item>();
	private ItemAdapter adapter;
	private String xmlFile = "list_item.xml";
	
	private int mType = TYPE_HOME;
	public static final int TYPE_HOME = 0x0;
	
	public CategoryScr() {
		mType = TYPE_HOME;
	}
	
	public CategoryScr(int type) {
		mType = type;
	}
	
	public CategoryScr(String previousScrTag, int type) {
		super(previousScrTag);
		mType = type;
	}
	
	public CategoryScr(int type, String gd) {
		mType = type;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if(mLayout == null) {
			mLayout = (ViewGroup) inflater.inflate(R.layout.category_scr_layout, container, false);
			Log.i(TAG, getPreviousScrTag() + getTag() + mType);
			switch (mType) {
			case TYPE_HOME:
				setUp();
				break;
			default:
				setUp();
				break;
			}
		} else {
			((ViewGroup) mLayout.getParent()).removeView(mLayout);
		}
		return mLayout;
	}
	
	private void parseXML() {
		try {
			InputStream in = getActivity().getAssets().open(xmlFile);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(in);
			doc.getDocumentElement().normalize();
			NodeList nList = doc.getElementsByTagName("item");
			for (int i = 0; i < nList.getLength(); i ++) {
				Node node = nList.item(i);
				Item item = new Item();
				for (int j = 0; j < node.getChildNodes().getLength(); j ++) {
					Node tmp = node.getChildNodes().item(j);
					if (tmp.getNodeName().equalsIgnoreCase("id")) {
						item.setId(tmp.getTextContent());
						//System.out.println("--- ID: " + tmp.getTextContent());
					} else if (tmp.getNodeName().equalsIgnoreCase("name")) {
						item.setName(tmp.getTextContent());
						//System.out.println("--- NAME: " + tmp.getTextContent());
					}
				}
				this.items.add(item);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void setUp() {
		final ListView listview = (ListView) mLayout.findViewById(android.R.id.list);
		parseXML();
        adapter = new ItemAdapter(getActivity(), this.items);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(getActivity(), ChapterActivity.class);
				intent.putExtra("position", position);
				intent.putExtra("items", getItems());
				startActivity(intent);
			}
		});
		adView = (AdView) mLayout.findViewById(R.id.adView);
		AdRequest adRequest = new AdRequest();
        adView.loadAd(adRequest);
	}
	
	@Override
	public void onSearchComplete(final CharSequence in) {
	}

	public ArrayList<Item> getItems() {
		return items;
	}
}
